package com.nc.user.service.impl;

import com.nc.user.service.PermissionsService;
import com.plug.BeanUtil;
import com.plug.result.RetResponse;
import com.plug.result.RetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.nc.user.dao.FrmPermissionsDao;
import com.nc.user.model.dto.PermissionsDto;
import com.nc.user.model.entity.FrmPermissions;
import com.nc.user.service.PermissionsService;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@Service
public class PermissionsServiceImpl implements PermissionsService {
    @Autowired
    private FrmPermissionsDao permissionsDao;

    @Transactional
    @Override
    public RetResult addPermissions(PermissionsDto permissionsDto) {
        try {
            permissionsDto.getPermissionsVos().forEach(per -> {
                permissionsDao.save(BeanUtil.convertToBean(per, FrmPermissions.class));
            });
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RetResponse.makeErrRsp("失败");
        }
    }

    @Transactional
    @Override
    public RetResult deleteById(String uuid) {
        try {
            permissionsDao.deleteById(uuid);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp("保存失败");
        }
    }

    @Override
    public RetResult findAll() {
        return RetResponse.makeOKRsp(permissionsDao.findAll());
    }

    @Override
    public List<FrmPermissions> findByRoles(List<String> roleCodes) {
        return permissionsDao.findByFrmRolesCode(roleCodes);
    }
}
